local skel = fk.CreateSkill {
  name = "emo__shuliu",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__shuliu"] = "漱流",
  [":emo__shuliu"] = "当你使用一张转化牌后，若此牌曾为你从其他角色区域里获得的牌，你可对一名本回合失去过牌的其他角色造成1点伤害，然后令该角色获得出牌阶段各限一次的“暗取”直到其回合结束。一名角色获得或失去一个技能后，你摸一张牌。",

  ["@@emo__shuliu-inhand"] = "漱流",
  ["#emo__shuliu-choose"] = "漱流：你可以对1名失去过牌的角色造成1点伤害，令其获得临时“暗取”直到其回合结束",
}

skel:addEffect(fk.CardUseFinished, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return data.extra_data and data.extra_data.emo__shuliu
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.from and move.from ~= player and table.find(move.moveInfo, function (info)
          return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
        end) then
          table.insertIfNeed(targets, move.from)
        end
      end
    end, Player.HistoryTurn)
    targets = room:deadPlayerFilter(targets)
    if #targets == 0 then return false end
    local tos = room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = targets, skill_name = skel.name,
      prompt = "#emo__shuliu-choose",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:damage({
      from = player,
      to = to,
      damage = 1,
      skill = skel.name,
    })
    if to.dead then return end
    room:handleAddLoseSkills(to, "emo__anqu_copy")
  end,
})

--- 从其他角色区域内获得牌时，加上标记
skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if not player:hasSkill(skel.name, true) then return false end
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand and move.from and move.from ~= player then
        return true
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand and move.from and move.from ~= player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea ~= Card.PlayerSpecial and table.contains(player:getCardIds("h"), info.cardId) then
            player.room:setCardMark(Fk:getCardById(info.cardId), "@@emo__shuliu-inhand", 1)
          end
        end
      end
    end
  end,
})

-- 使用牌前，检测是否满足触发条件
skel:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name, true) and data.card:isVirtual() then
      local cards = Card:getIdList(data.card)
      return #cards > 0 and table.every(cards, function (id)
        return Fk:getCardById(id):getMark("@@emo__shuliu-inhand") ~= 0
      end)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.emo__shuliu = true
  end,
})

skel:addEffect(fk.EventAcquireSkill, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and data.skill:isPlayerSkill(data.who) and player.room:getBanner("RoundCount")
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, skel.name)
  end,
})

skel:addEffect(fk.EventLoseSkill, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and data.skill:isPlayerSkill(data.who) and player.room:getBanner("RoundCount")
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, skel.name)
  end,
})

return skel
